<?php
/**
 * Description: 获取知识点任务学前学后的真题
 * User: devforma
 * Date: 15/1/14
 * Time: 16:36
 */

require __DIR__ . '/includes/utils.php';

class GetTaskQuestions implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        //参数校验
        if (empty($queryArgs['task_id']) || !filter_var($queryArgs['task_id'], FILTER_VALIDATE_INT)
            || !in_array($queryArgs['type'], ['xh', 'xq'], true)) {
            return genOutput(1000);
        }

        //获取任务记录
        $task = DailyPlanUtils::getMissionRecord($queryArgs['task_id'], validateUser($queryArgs) !== 1);
        if (empty($task)) return genOutput(1000);

        //解析任务内容
        $content = json_decode($task->mission_content);
        
        if ($task->action_type == 0) { //任务未完成
            $ids = $content->questions->{$queryArgs['type']};
            $list = getQuestionsByIds($ids);

            return genOutput(1, [
                'done' => false,
                'questions' => $list,
                'answers' => []
            ]);

        } else { //任务已完成
            $ids = [];
            foreach ($content->questions->{$queryArgs['type']} as $q) {
                $ids[] = $q->id;
            }
            $list = getQuestionsByIds($ids);

            return genOutput(1, [
                'done' => true,
                'questions' => $list,
                'answers' => $content->questions->{$queryArgs['type']}
            ]);
        }
    }
}